<template>
  <a-modal :title="$t('套餐详情')" :visible="visible" @cancel="handleCancel" width="50%" class="modal_title" v-drag>
    <div class="wrapper">
      <a-row>
        <a-col>
          <a-table
            bordered
            class="j-table-force-nowrap"
            ref="table"
            rowKey="id"
            :columns="columns"
            :dataSource="goodsList"
            :pagination="pagination"
          >
          </a-table>
        </a-col>
      </a-row>
    </div>

    <template slot="footer">
      <a-button @click="handleCancel">{{ $t('关闭') }}</a-button>
    </template>
  </a-modal>
</template>

<script>
import { postAction, getAction } from '@/api/manage'
export default {
  name: 'TypeNameDetailModal',
  components: {},
  data() {
    return {
      visible: false,
      columns: [
        {
          title: this.$t('商品名称'),
          align: 'center',
          dataIndex: 'goodsName',
        },
        {
          title: this.$t('单价'),
          align: 'center',
          dataIndex: 'price',
          customCell: () => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('数量'),
          align: 'center',
          dataIndex: 'amount',
          customCell: () => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('金额'),
          align: 'center',
          dataIndex: 'payAmount',
          customCell: () => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, record) => {
            return record.payAmount.toFixed(2)
          },
        },
      ],
      goodsList: [],
      pagination: false,
      recordsAmount: '',
      infoData: {},
      isTax: localStorage.getItem('taxSetting'),
    }
  },
  methods: {
    edit(record, list) {
      this.columns = [
        {
          title: this.$t('商品名称'),
          align: 'center',
          dataIndex: 'goodsName',
        },
        {
          title: this.$t('单价'),
          align: 'center',
          dataIndex: 'price',
          customCell: () => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('数量'),
          align: 'center',
          dataIndex: 'amount',
          customCell: () => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('金额'),
          align: 'center',
          dataIndex: 'payAmount',
          customCell: () => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, record) => {
            return record.payAmount.toFixed(2)
          },
        },
      ]
      this.isTax = localStorage.getItem('taxSetting')
      if (this.isTax == '1') {
        this.columns.splice(3, 0, {
          title: this.$t('税费'),
          align: 'center',
          dataIndex: 'taxPrice',
          width: 96,
          customRender: (text, record) => {
            return record.taxPrice.toFixed(2)
          },
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
                textAlign: 'right',
              },
            }
          },
        })
      }
      this.visible = true
      this.goodsList = []
      let arr = list.filter((f) => {
        return f.parentId == record.id
      })
      this.goodsList = Object.assign([], arr)
      this.addSumRow(['goodsName', 'amount', 'taxPrice', 'payAmount'], this.goodsList)
    },
    addSumRow(myArr, dataSource) {
      let total = {}
      myArr.forEach((tcn, ind) => {
        if (ind == 0) {
          total[tcn] = this.$t('合计')
        } else {
          total[tcn] = 0
        }
      })
      for (let key in total) {
        dataSource.forEach(function (ds) {
          if (!isNaN(total[key])) {
            total[key] += Number(ds[key])
          }
        })
      }
      dataSource.push(total)
    },
    handleCancel() {
      this.visible = false
    },
  },
}
</script>
<style>
</style>